package com.zzyl.controller.app;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.entity.Reservation;
import com.zzyl.service.ReservationService;
import com.zzyl.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class AppReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    //查询取消预约数量
    @GetMapping("/customer/reservation/cancelled-count")
    public ResponseResult getCancelCount()
    {
        int count = reservationService.getCancelCount();
        return success(count);
    }

    //查询剩余预约次数,一个时间段只允许预约六次
    @GetMapping("/customer/reservation/countByTime")
    public ResponseResult getRemainingCount(Long time){
        List<TimeCountVo> list = reservationService.getRemainingCount(time);
        return success(list);
    }

    //新增预约记录
    @PostMapping("/customer/reservation")
    public ResponseResult save(@RequestBody Reservation reservation){
        reservationService.saveReservation(reservation);
        return success();
    }

    //分页查询预约
    @GetMapping("/customer/reservation/page")
    public ResponseResult getReservation(Integer status,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        PageResponse<Reservation> page = reservationService.findByIdPage(status,pageNum,pageSize);
        return success(page);
    }

    //取消预约
    @PutMapping("/customer/reservation/{id}/cancel")
    public ResponseResult cancelReservation(@PathVariable Long id){
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        reservationService.updateById(reservation);
        return success();
    }
}
